/**
 * 
 */
package org.shanktech.tshare.binlogsync.process.parser;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author 6407001387
 *
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public abstract class ObjectParser {

  public Object parser(String[] columnNames, Object[] columnValues) {
    if (null == columnNames || null == columnValues) {
      return null;
    }
    
    Class objectClass = getClassObject();
    Object object = getInstance();
    for (int i = 0; i < columnNames.length; i++) {
      Field item;
      try {
        item = objectClass.getDeclaredField(columnNames[i]);
        Method method = objectClass
            .getMethod(getSetFunctionName(columnNames[i]), item.getType());
        method.invoke(object,
            columnValueProcess(columnNames[i], columnValues[i]));

      } catch (Exception e) {
      }

    }
    return object;
  }

  private String getSetFunctionName(String propertieName) {
    StringBuilder sb = new StringBuilder();
    sb.append("set").append(propertieName.substring(0, 1).toUpperCase())
        .append(propertieName.substring(1));
    return sb.toString();
  }

  public Object columnValueProcess(String name, Object value) {
    return value;
  }

  public abstract Class<?> getClassObject();

  public abstract Object getInstance();
}
